#include "dataParser.h"
#include <cstring>
#include <cjson/cJSON.h>

INFO_CODE parseToString(cJSON *json, char *string, char *name)
{
    cJSON *j_str = NULL;
    if ((j_str = cJSON_GetObjectItem(json, name)) == NULL)
        return ERROR;
    strcpy(string, j_str->valuestring);
    return ACCEPTED;
}


int encodeLoginInfo(char *user_name, char *password, cJSON *login_info)
{
    cJSON_AddStringToObject(login_info, "type", "login_info");
    cJSON_AddStringToObject(login_info, "user_name", user_name);
    cJSON_AddStringToObject(login_info, "password", password);

    return ACCEPTED;
}

int parseLoginInfo(cJSON *login_info, char *user_name, char *password)
{
    INFO_CODE info;
    if ((info = parseToString(login_info, user_name, "user_name")) != ACCEPTED)
        return info;
    if ((info = parseToString(login_info, password, "password")) != ACCEPTED)
        return info;

    return ACCEPTED;
}